package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.entity.*;
import com.hp.result.DataGridResultInfo;
import com.hp.result.SubmitResultInfo;
import com.hp.service.MenusService;
import com.hp.service.PowerService;
import com.hp.service.RoleService;
import com.hp.utils.JacksonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Power(name = "角色管理",type =0)
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenusService menusService;
    @Autowired
    private PowerService powerService;

    /**
     * 查询所有
     *
     * @return
     */
    @Power(name = "查询所有",type =1,url = "/role/queryrole_result")
    @RequestMapping("/queryrole_result")
    @ResponseBody
    public DataGridResultInfo queryrole_result(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows) throws Exception {

        return roleService.page(page, rows);

    }

    /**
     * 去查询页面
     *
     * @return
     */
    @Power(name = "去查询页面",type =1,url = "/role/queryrole")
    @GetMapping(value = "/queryrole")
    public String queryrole() {
        return "role/queryrole";
    }

    /**
     * 去添加页面
     *
     * @return
     */
    @Power(name = "去添加页面",type =1,url = "/role/toaddrole")
    @GetMapping("/toaddrole")
    public String toaddrole() {
        return "role/addrole";
    }

    @Power(name = "添加角色",type =1,url = "/role/addrolesubmit")
    @ResponseBody
    @PostMapping("/addrolesubmit")
    public SubmitResultInfo addRolesubmit(Role role) {
        return roleService.addRole(role);
    }

    @Power(name = "修改角色页面",type =1,url = "/role/editrole")
    @RequestMapping("/editrole")
    public String editrole(int id, Map<String, Object> map) throws Exception {
        Role role = roleService.findById(id);
        map.put("role", role);
        return "role/editrole";
    }

    @Power(name = "修改角色",type =1,url = "/role/editrolesubmit")
    @RequestMapping(value = "/editrolesubmit", method = RequestMethod.POST)
    @ResponseBody
    public SubmitResultInfo editrolesubmit(Role role) throws Exception {
        return roleService.updateRole(role);
    }

    /**
     * 去菜单授权页面
     *
     * @return
     */
    @Power(name = "去菜单授权页面",type =1,url = "/role/toauthmenus")
    @GetMapping("/toauthmenus")
    public String toauthmenus(int id, Model model) {
        System.out.println("id:d"+id);
        List<Menus> list = menusService.findAllByRoleId(id);//通过id查询对应的菜单
        List<MenusTreeVo> parent = menusService.findAllByParent(list);


        model.addAttribute("roleid", id);
        model.addAttribute("menus", JacksonUtil.list2Json(parent));
        return "role/authmenus";

    }

    /**
     * 菜单授权
     * @return
     * 
     */
    @Power(name = "菜单授权",type =1,url = "/role/authmenus")
    @ResponseBody
    @PostMapping("/authmenus")
    public SubmitResultInfo authmenus(@RequestBody List<RoleMenusVo> roleMenusVo) {

       return menusService.authmenusByRole(roleMenusVo);
    }

    /**
     * 去功能授权页面
     *
     * @return
     */
    @Power(name = "去功能授权页面",type =1,url = "/role/toauthpower")
    @GetMapping("/toauthpower")
    public String toauthpower(int id, Model model) {
        List<com.hp.entity.Power> list = powerService.findAllByRoleId(id);//通过id查询对应角色有的功能
        List<PowerTreeVo> parent = powerService.findAllByParent(list);

        model.addAttribute("roleid", id);
        model.addAttribute("powers", JacksonUtil.list2Json(parent));
        return "role/authpower";
    }

    /**
     * 功能授权
     * @return
     *
     */
    @Power(name = "功能授权",type =1,url = "/role/authpower")
    @ResponseBody
    @PostMapping("/authpower")
    public SubmitResultInfo authpower(@RequestBody List<RolePowerVo> rolePowerVos) {

        return powerService.authPowerByRole(rolePowerVos);
    }
}
